package handlers

import (
	"github.com/rs/xid"
	"github.com/shugen/models"
	"github.com/shugen/utils"
	"net/http"
	"strconv"
	"time"
)

//门天干关系
func (c *Controller) IMentgan(w http.ResponseWriter, r *http.Request) {

	if r.Method != "POST" {
		return
	}

	type_value, _ := strconv.Atoi(r.PostFormValue("type"))
	mentgan := &models.MenTgan{
		MenTganId: []byte(xid.New().String()),
		Type:      type_value,
		Title:     r.PostFormValue("title"),
		Desc:      r.PostFormValue("desc"),
		Status:    1,
		CreatedAt: time.Now().UTC(),
		UpdatedAt: time.Now().UTC(),
	}
	if err := c.Svc.InsertMenTgan(mentgan); err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(mentgan))

}

//update 门天干关系
func (c *Controller) UMentgan(w http.ResponseWriter, r *http.Request) {

	mtg := &models.MenTgan{
		MenTganId: []byte(r.PostFormValue("MenTganId")),
		//Type:      0,
		Title:  r.PostFormValue("title"),
		Desc:   r.PostFormValue("desc"),
		Status: 0,
		//CreatedAt: time.Time{},
		UpdatedAt: time.Now().UTC(),
	}

	mentgan, err := c.Svc.UpdateMenTgan(mtg)

	if err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(mentgan))
}

//select 门天干关系
func (c *Controller) SMentgan(w http.ResponseWriter, r *http.Request) {

	mx, err := c.Svc.SelectMenTgan(&models.MenTgan{MenTganId: []byte(r.PostFormValue("MenTganId"))})

	if err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(mx))
}

//select List 门星关系
func (c *Controller) LMentgan(w http.ResponseWriter, r *http.Request) {

	mtg, err := c.Svc.ListMenTgan()

	if err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(mtg))
}
